Efficiency Table:

NotationNameExample
O(1)ConstantArray access
O(log n)LogarithmicBinary search
O(n)LinearLinear scan
O(n log n)LinearithmicMerge sort
O(n²)QuadraticBubble sort

Hashtables & Dicts:

StructureSearchInsertDelete
Unsorted arrayO(n)O(1)O(n)
Sorted arrayO(log n)O(n)O(n)
Balanced BSTO(log n)O(log n)O(log n)
Hash TableO(1) avgO(1) avgO(1) avg

Collision:

When two hash keys refer to the same slot:
Handled through:

Open Addressing:

  • probe for the next empty slot (linear probing, quadratic probing) aka searching

Chaining:

  • each slot holds a linked list. Search is O(1 + load factor α), where α = n/m

Heapsort

Heap: sorted binary tree
Heapmax: parent value > child value

Key insight: Guaranteed O(n log n) always, in-place. But slower in practice than quicksort due to cache behavior. Building the heap is O(n), not O(n log n)

Sorting Comparison:

AlgorithmBest caseAverage caseWorst caseSpaceIn-place?Stable?Key weakness
Bubble sort

swap adjacent pairs
O(n)O(n²)O(n²)O(1)yesyesVery slow, mostly academic
Insertion sort

build sorted left side
O(n)O(n²)O(n²)O(1)yesyesSlow on large unsorted arrays
Selection sort

find min, place it
O(n²)O(n²)O(n²)O(1)yesnoAlways O(n²), no best case
Heap sort

max-heap + extract
O(n log n)O(n log n)O(n log n)O(1)yesnoPoor cache behavior, slower in practice
Merge sort

divide, sort, merge
O(n log n)O(n log n)O(n log n)O(n)noyesNeeds O(n) extra memory